package com.ujn.demo.controller;

import com.ujn.demo.service.OperateService;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @author sunjx
 * @program giteeCode
 * @description UploadTestController
 * @date 2020-09-29 10:20
 */
@Api(tags="swift2.0 测试")
@RestController
@RequestMapping("/swift")
public class OperateController {

    @Resource
    private OperateService operateService;

    private static final Logger logger = LoggerFactory.getLogger(OperateController.class);

    @GetMapping("/getToken")
    public String getToken(){
        String token = operateService.getNewToken();
        logger.info("token:{}" + token);
        return token;
    }

    @PostMapping("/uploadAndDelete")
    public String uploadAndDelete(@RequestParam(value = "objectName") String objectName,
                         @RequestParam(value = "containerName") String containerName,
                         @RequestParam(value = "multipartFile") MultipartFile multipartFile){
        String token = operateService.getNewToken();
        String result = operateService.uploadAndDelete(objectName, containerName, token, multipartFile);
        logger.info("uploadAndDeleteresult:{}", result);
        return result;
    }

    @PostMapping("/upload")
    public String upload(@RequestParam(value = "objectName") String objectName,
                                    @RequestParam(value = "containerName") String containerName,
                                    @RequestParam(value = "multipartFile") MultipartFile multipartFile){
        String token = operateService.getNewToken();
        String result = operateService.uploadObjectByStream(objectName, containerName, token, multipartFile);
        logger.info("上传的result:{}", result);
        return result;
    }

    @GetMapping("/download")
    public String download(@RequestParam(value = "objectName") String objectName,
                                     @RequestParam(value = "containerName") String containerName){
        String token = operateService.getNewToken();
        String result = operateService.downloadObjectByStream(containerName, objectName, token);
        logger.info("下载的result:{}", result);
        return result;
    }

    @GetMapping("/getContainerList")
    public String getSwiftContainerList(){
        String token = operateService.getNewToken();
        String result = operateService.getSwiftContainerList(token);
        logger.info("getContainerList的result:{}", result);
        return result;
    }

    @PostMapping("/create")
    public String createContainer(@RequestParam(value = "containerName") String containerName){
        String token = operateService.getNewToken();
        String result = operateService.createContainer(token, containerName);
        logger.info("getContainerList的result:{}", result);
        return result;
    }

    @DeleteMapping("/deleteContainer")
    public String deleteContainer(@RequestParam(value = "containerName") String containerName){
        String token = operateService.getNewToken();
        String result = operateService.deleteContainerIfEmpty(token, containerName);
        logger.info("deleteContainer的result:{}", result);
        return result;
    }

    @GetMapping("/getSwiftObjectList")
    public String  getSwiftObjectList(@RequestParam(value = "containerName") String containerName){
        String token = operateService.getNewToken();
        String result = operateService.getSwiftObjectList(containerName, token);
        logger.info("getSwiftObjectList的result:{}", result);
        return result;
    }

    @DeleteMapping("/deleteObject")
    public String deleteObject(@RequestParam(value = "containerName") String containerName, @RequestParam(value = "objName") String objName){
        String token = operateService.getNewToken();
        String result = operateService.deleteObject(containerName, objName, token);
        logger.info("deleteObject的result:{}", result);
        return result;
    }
}
